//
//  OilMarketVC.m
//  Base_App
//
//  Created by Yutian Duan on 2019/5/30.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "OilMarketVC.h"
#import "OilMapVC.h"
#import "OilMarketListTableView.h"
#import "OilMarketListTitleHeadView.h"

@interface OilMarketVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) OilMarketListTableView *tableView;

@property (nonatomic, copy) NSArray *dataArray;

@end

@implementation OilMarketVC

- (void)viewDidLoad {
  [super viewDidLoad];


}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  NSLog(@"市场咨询");
}

- (void)createFields {
  [super createFields];
  
  OilMarktLayout *layout1 = [[OilMarktLayout alloc] init];
  layout1.logoImage = [UIImage imageNamed:@"Oil_Market_PriceLogo"];
  layout1.title = YTLocalizedString(@"油价行情");
  layout1.updateTime = [NSString stringWithFormat:@"%@:  %@",YTLocalizedString(@"最后更新于"),@"2019 12:00:00"];
  [layout1.optionArray addObject:@""];
  [layout1.optionArray addObject:@""];
  [layout1.optionArray addObject:@""];
  [layout1.optionArray addObject:@""];
  [layout1.optionArray addObject:@""];
  ///
  layout1.canOpen = YES;
  layout1.cellHeight = 44.0f;
  
  OilMarktLayout *layout2 = [[OilMarktLayout alloc] init];
  layout2.logoImage = [UIImage imageNamed:@"Oil_Market_PreferentialLogo"];
  layout2.title = YTLocalizedString(@"优惠对比");
  ///
  layout2.cellHeight = 0;
  
  OilMarktLayout *layout3 = [[OilMarktLayout alloc] init];
  layout3.logoImage = [UIImage imageNamed:@"Oil_Market_Map"];
  layout3.title = YTLocalizedString(@"油站地图");
  [layout3.optionArray addObject:@""];
  //
  layout3.cellHeight = (ScreenWidth - 40) * ITEMPRO + 50;
  
  _dataArray = @[layout1,layout2,layout3];

  
}

- (void)createViews {
  [super createViews];
  
  _tableView = [[OilMarketListTableView alloc] initWithFrame:CGRectMake(0, 44, ScreenWidth, ScreenHeight - NavBarHeight - TabBarHeight - 34) style:UITableViewStylePlain];
  _tableView.delegate = self;
  _tableView.dataSource = self;
  _tableView.backgroundColor = HEXCOLOR(0xf7f8fc);
  [self.view addSubview:_tableView];
  

  
}

- (void)loadData {
  [super loadData];
  
  
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  
  OilMarketListTitleHeadView *head = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"OilMarketListTitleHeadView"];
  if (!head) {
    head = [[OilMarketListTitleHeadView alloc] initWithReuseIdentifier:@"OilMarketListTitleHeadView"];
    
  }

  head.layout = _dataArray[section];
  
  __weak typeof(self) weakSelf = self;
  head.reloadBlock = ^{
    if (section == 0) {
      //
      OilMarktLayout *layout = weakSelf.dataArray[section];
      if (layout.canOpen == YES) {
        layout.isOpen = !layout.isOpen;
      }
      [weakSelf.tableView reloadData];
    } else if (section == 1) {
      
      
    } else if (section == 2) {
      OilMapVC *mapVC = [[OilMapVC alloc] init];
      mapVC.hidesBottomBarWhenPushed = YES;
      [weakSelf.navigationController pushViewController:mapVC animated:YES];
    }
  };
  
  return head;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

  OilMarktLayout *layout = _dataArray[indexPath.section];
  return layout.cellHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  OilMarktLayout *layout = _dataArray[section];
  if (layout.canOpen) {
    return layout.isOpen?layout.optionArray.count:0;
  } else {
    return layout.optionArray.count;
  }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  if (indexPath.section == 0) {
    ///! 油价
    OilPriceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OilPriceListCell"];
    if (!cell) {
      cell = [[OilPriceListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OilPriceListCell"];
    }
    [cell setupData];
    if (indexPath.row % 2 == 0) {
      ///
      cell.backgroundColor = HEXCOLOR(0xfbf1f0);
    } else {
      cell.backgroundColor = HEXCOLOR(0xdedede);
    }
    return cell;
    
  } else if (indexPath.section == 1) {
    return [[UITableViewCell alloc] init];
  } else {
    ///! 咨询
    OilMarketListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OilMarketListCell"];
    if (!cell) {
      cell = [[OilMarketListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OilMarketListCell"];
    }
    [cell setupData];
    return cell;
  }
  
  
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
  
}


@end

